"use strict";
const { Model } = require("sequelize");
const bcrypt = require("bcryptjs");
module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      models.Category.hasMany(models.Course, {
        as: "courses",
      })
    }
  }
  User.init(
    {
      email: {
        type: DataTypes.STRING,
        // unique: true,
        allowNull: false,  // 这里添加 allowNull: false
        validate: {
          notNull: {
            msg: "邮箱必须填写",
          },
          notEmpty: {
            msg: "邮箱不能为空",
          },
          isEmail: {
            msg: "邮箱格式不正确",
          },

          async isUnique(value) {
            const user = await User.findOne({ where: { email: value } });
            if (user) {
              throw new Error("邮箱已存在");
            }
          },
        },
      },
      username: {
        type: DataTypes.STRING,
        allowNull: false, 
        validate: {
          notNull: {
            msg: "用户名必须填写",
          },
          notEmpty: {
            msg: "用户名不能为空",
          },
          len: {
            args: [2, 45],
            msg: "用户名长度在2到20之间",
          },
          async isUnique(value) {
            const user = await User.findOne({ where: { username: value } });
            if (user) {
              throw new Error("用户名已存在");
            }
          },
        },
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false, 
        validate: {
          notNull: {
            msg: "密码不能为空",
          },
          notEmpty: {
            msg: "密码不能为空",
          },
          // len: {
          //   args: [2, 45],
          //   msg: "密码长度在2到20之间",
          // },
          set(value) {

            if(value.length>=2&& value.length<=45){
              // 加密
              this.setDataValue("password", bcrypt.hashSync(value, 10));
            }else{
              throw new Error("密码长度在2到45之间")
            }
          }
        },
      },
      avatar: {
        type: DataTypes.STRING,
        allowNull: false, 
        validate: {
          isUrl: {
            msg: "图片地址不正确",
          },
        },
      },
      nickname: DataTypes.STRING,
      sex: {
        type: DataTypes.TINYINT,
        allowNull: false, 
        validate: {
          notNull: {
            msg: "性别不能为空",
          },
          notEmpty: {
            msg: "性别不能为空",
          },
          isIn: {
            args: [[0, 1, 3]],
            msg: "性别只能是男:0,女:1",
          },
        },
      },
      company: DataTypes.STRING,
      introduction: DataTypes.STRING,
      role: {
        type: DataTypes.TINYINT,
        allowNull: false,
        validate: {
          notNull: {
            msg: "用户角色不能为空",
          },
          notEmpty: {
            msg: "用组不能为空",
          },
          isIn: {
            args: [[0, 100]],
            msg: "用户角色只能是,普通用户:0,管理员:100",
          },
        },
      },
    },
    {
      sequelize,
      modelName: "User",
    }
  );
  return User;
};
